﻿using System;
using System.Windows.Forms;
using System.IO;
using ZeldoEngineObjects.Maps;
using ZeldoEngineServices.Maps;

namespace MapEditor.Windows.EditorCreateMap
{
    public partial class MapNew : Form
    {
        public delegate void SubmitMapHandler(MapModel mapSubmited);
        public event SubmitMapHandler SubmitMap;
        
        public MapNew()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.Validation())
            {
                var mapName = this.textBoxTitre.Text;
                var width = (int)this.numericUpDownWidth.Value;
                var height = (int)this.numericUpDownHeight.Value;
                var nomChipset = this.comboBoxChipset.Text;

                var chipset = AppParams.GetIns().Project.Ressources.Chipsets.LireChipset(nomChipset);

                var map = MapService.CreerCarte(mapName, width, height, chipset);

                if (this.SubmitMap != null)
                {
                    this.SubmitMap(map);
                }

                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.SubmitMap != null)
            {
                this.SubmitMap(null);
            }
            this.Close();
        }

        private bool Validation()
        {
            return this.textBoxTitre.Text != string.Empty;
        }

        private void MapNew_Load(object sender, EventArgs e)
        {
            foreach (string chips in AppParams.GetIns().Project.LireListeChipsets())
            {
                this.comboBoxChipset.Items.Add(Path.GetFileNameWithoutExtension(chips));
            }

            if (this.comboBoxChipset.Items.Count > 0)
            {
                this.comboBoxChipset.SelectedIndex = 0;
            }

            this.textBoxTitre.Text = "Carte " + AppParams.GetIns().Project.Registres.Cartes.Count;
        }
    }
}
